﻿using System;
using System.Windows.Input;
using Oxage.Models;
using Oxage.Services;

namespace Oxage.Commands
{
	public class DocumentCommand : ICommand
	{
		#region Public properties
		public DocumentViewModel Document
		{
			get;
			set;
		}
		#endregion

		#region ICommand Members
		public bool CanExecute(object parameter)
		{
			if (this.Document == null)
			{
				return false;
			}

			var command = parameter as string;
			switch (command)
			{
				case "Save":
					return this.Document.CanSaveWithoutDialog;
			}

			return true;
		}

		public event EventHandler CanExecuteChanged;
		protected virtual void OnCanExecuteChanged()
		{
			if (CanExecuteChanged != null)
			{
				CanExecuteChanged(this, new EventArgs());
			}
		}

		public void Execute(object parameter)
		{
			if (this.Document == null)
			{
				return;
			}

			var command = parameter as string;
			switch (command)
			{
				case "New":
					DocumentService.NewFile();
					break;

				case "Open":
					DocumentService.OpenFile();
					break;

				case "Save":
					DocumentService.SaveFile(false);
					break;

				case "SaveAs":
					DocumentService.SaveFile(true);
					break;

				case "Import":
					this.Document.ImportDialog();
					break;

				case "Export":
					this.Document.ExportDialog();
					break;
			}
		}
		#endregion
	}
}
